﻿(function(){function f(){this.posFrom=this.posTo=this.query=null;this.marked=[]}function g(a,b,c){return a.getSearchCursor(b,c,"string"==typeof b&&b==b.toLowerCase())}function h(a,b,c,d){a.openDialog?a.openDialog(b,d):d(prompt(c,""))}function n(a,b,c,d){if(a.openConfirm)a.openConfirm(b,d);else if(confirm(c))d[0]()}function k(a){var b=a.match(/^\/(.*)\/([a-z]*)$/);return b?RegExp(b[1],-1==b[2].indexOf("i")?"":"i"):a}function i(a,b){var c=a._searchState||(a._searchState=new f);if(c.query)return l(a,
b);h(a,o,"Search for:",function(d){a.operation(function(){if(d&&!c.query){c.query=k(d);if(2E3>a.lineCount())for(var e=g(a,c.query);e.findNext();)c.marked.push(a.markText(e.from(),e.to(),{className:"CodeMirror-searching"}));c.posFrom=c.posTo=a.getCursor();l(a,b)}})})}function l(a,b){a.operation(function(){var c=a._searchState||(a._searchState=new f),d=g(a,c.query,b?c.posFrom:c.posTo);if(!d.find(b)&&(d=g(a,c.query,b?{line:a.lineCount()-1}:{line:0,ch:0}),!d.find(b)))return;a.setSelection(d.from(),d.to());
c.posFrom=d.from();c.posTo=d.to()})}function j(a){a.operation(function(){var b=a._searchState||(a._searchState=new f);if(b.query){b.query=null;for(var c=0;c<b.marked.length;++c)b.marked[c].clear();b.marked.length=0}})}function m(a,b){h(a,p,"Replace:",function(c){c&&(c=k(c),h(a,q,"Replace with:",function(d){if(b)a.operation(function(){for(var b=g(a,c);b.findNext();)if("string"!=typeof c){var e=a.getRange(b.from(),b.to()).match(c);b.replace(d.replace(/\$(\d)/,function(a,b){return e[b]}))}else b.replace(d)});
else{j(a);var e=g(a,c,a.getCursor()),f=function(){var b=e.from(),d;if(!(d=e.findNext()))if(e=g(a,c),!(d=e.findNext())||b&&e.from().line==b.line&&e.from().ch==b.ch)return;a.setSelection(e.from(),e.to());n(a,r,"Replace?",[function(){h(d)},f])},h=function(a){e.replace("string"==typeof c?d:d.replace(/\$(\d)/,function(b,c){return a[c]}));f()};f()}}))})}var o='Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>',p='Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>',
q='With: <input type="text" style="width: 10em"/>',r="Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";CodeMirror.commands.find=function(a){j(a);i(a)};CodeMirror.commands.findNext=i;CodeMirror.commands.findPrev=function(a){i(a,!0)};CodeMirror.commands.clearSearch=j;CodeMirror.commands.replace=m;CodeMirror.commands.replaceAll=function(a){m(a,!0)}})();